<template>
    <div class="product-detail">
        <SimpleHeader :name="'商品详情'"></SimpleHeader>
        <div class="detail-content">
            <div class="detail-swipe-wrap">
                <van-swipe class="my-swipe" :autoplay="3000" lazy-render>
                    <van-swipe-item v-for="image in state.detail.goodsCarouselList" :key="image">
                        <img :src="image" />
                    </van-swipe-item>
                </van-swipe>
            </div>
            <div class="product-info">
                <div class="product-title"> {{ state.detail.goodsName }}</div>
                <div class="product-y">免邮费 顺风快递</div>
                <div class="product-price">
                    ￥{{ state.detail.sellingPrice }}
                </div>
            </div>
            <div class="product-intro">
                <ul>
                    <li>概述</li>
                    <li>参数</li>
                    <li>安装服务</li>
                    <li>常见问题</li>
                </ul>
                <div class="product-content" v-html="state.detail.goodsDetailContent"></div>
            </div>
            <van-action-bar>
                <van-action-bar-icon icon="chat-o" text="客服"></van-action-bar-icon>
                <van-action-bar-icon icon="cart-o" text="购物车" :badge="!store.state.count ? '' : store.state.count" @click="goTo"></van-action-bar-icon>
                <van-action-bar-button type="warning"  text="加入购物车" @click="addCartItem"></van-action-bar-button>
                <van-action-bar-button type="danger" text="立即购买"></van-action-bar-button>

            </van-action-bar>
        </div>
    </div>
    
</template>

<script setup>
import SimpleHeader from '../components/SimpleHeader.vue'
import { getDetail } from '@/service/goods'
import { addCart } from '@/service/cart'
import { onMounted, reactive } from 'vue'
import { useRoute ,useRouter } from 'vue-router';
import { useStore } from 'vuex'
import { showToast } from 'vant';

const store = useStore()
console.log(store);
const route = useRoute()
const router = useRouter()
const state = reactive({
    detail:{
        goodsCarouselList:[]
    }
})
onMounted(async()=>{
    const { id } = route.params
    const {data } = await getDetail(id)
    state.detail = data
    store.commit('updateCart')
})
const goTo = ()=>{
    router.push({path:'/cart'})
}
// 添加购物车
const addCartItem = async ()=>{
    const { resultCode } = await addCart({goodsCount:1,goodsId:state.detail.goodsId})
    // console.log(resultCode);
    if(resultCode == 200) showToast('添加成功')
    store.commit('updateCart')
}
</script>

<style lang="less">
  @import '../common/style/mixin';
  .product-detail {
    .detail-header {
      position: fixed;
      top: 0;
      left: 0;
      z-index: 10000;
      .fj();
      .wh(100%, 44px);
      line-height: 44px;
      padding: 0 10px;
      .boxSizing();
      color: #252525;
      background: #fff;
      border-bottom: 1px solid #dcdcdc;
      .product-name {
        font-size: 14px;
      }
    }
    .detail-content {
      height: calc(100vh - 50px);
      overflow: hidden;
      overflow-y: auto;
      .detail-swipe-wrap {
        .my-swipe .van-swipe-item {
          img {
            width: 100%;
            // height: 300px;
          }
        }
      }
      .product-info {
        padding: 0 10px;
        .product-title {
          font-size: 18px;
          text-align: left;
          color: #333;
        }
        .product-desc {
          font-size: 14px;
          text-align: left;
          color: #999;
          padding: 5px 0;
        }
        .product-price {
          .fj();
          span:nth-child(1) {
            color: #F63515;
            font-size: 22px;
          }
          span:nth-child(2) {
            color: #999;
            font-size: 16px;
          }
        }
      }
      .product-intro {
        width: 100%;
        padding-bottom: 50px;
        ul {
          .fj();
          width: 100%;
          margin: 10px 0;
          li {
            flex: 1;
            padding: 5px 0;
            text-align: center;
            font-size: 15px;
            border-right: 1px solid #999;
            box-sizing: border-box;
            &:last-child {
              border-right: none;
            }
          }
        }
        .product-content {
          padding: 0 20px;
          img {
            width: 100%;
          }
        }
      }
    }
    .van-action-bar-button--warning {
      background: linear-gradient(to right,#6bd8d8, @primary)
    }
    .van-action-bar-button--danger {
      background: linear-gradient(to right, #0dc3c3, #098888)
    }
  }
</style>